package com.foreveross.taskservice.engine.worker;

import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.junit.Test;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.foreveross.taskservice.core.domain.channel.GradChannelEntity;
import com.foreveross.taskservice.infra.cfg.PropertyUtils;
import com.foreveross.taskservice.infra.cfg.taskservice.TaskServicePropertiesLoader;
/**
 * 创建任务的worker
 * 完成从模版到任务记录的全过程,同时将任务推送到cache
 * 
 * @author Administrator
 *
 */
public class TaskCreateWorker extends AbstractTaskStatefulJobWorker {
	
	@Test
	@Override
	public void execute(JobExecutionContext context)throws JobExecutionException {
		long stime = System.currentTimeMillis();
		log.info("任务创建TaskCreateWorker开始....");
		List<GradChannelEntity>  channels = null;
		try {
			channels=getGradChannelApplication().getEnableChannel();
			for(GradChannelEntity c:channels){
				int tempSize=PropertyUtils.getIntProperty(
						TaskServicePropertiesLoader.TASKSERVICE_TASK_GETTEMPLATE_SIZE, TaskServicePropertiesLoader.FILE_NAME, 10);
				int cacheMaxSize=PropertyUtils.getIntProperty(
						TaskServicePropertiesLoader.TASKSERVICE_CACHE_WAIT_MAXSIZE, TaskServicePropertiesLoader.FILE_NAME,1000);
				int taskSize=getBuidBuildTaskToCacheApplication().buildTaskToCache(tempSize,c.getId(),cacheMaxSize);
				log.info(String.format("渠道[%s]本次创建任务数量[%s]",c.getName(), taskSize));	
			}
		} catch (Exception e) {
			log.error(ExceptionUtils.getFullStackTrace(e));
		}finally{
			channels = null;
			log.info("任务创建TaskCreateWorker结束....耗时ms:"+(System.currentTimeMillis()-stime));
		}
	}
}
